#include <cstdio>
#include <cstring>

int min( int a, int b ) { return ( a < b ) ?a :b; }
int v[ 1001 ][ 1001 ], a[ 1001 ], b[ 1001 ], f[ 1001 ], n;

void init( )
{
    int i;
    scanf("%d", &n);
    memset( v, 0, sizeof( v ) );
    for ( i = 0; i < n; i++ )
        scanf("%d", &a[ i ]);
    for ( i = 0; i < n; i++ )
        scanf("%d", &b[ i ]);
    for ( i = 0; i < n; i++ )
        v[ a[ i ] - 1 ][ n - b[ i ] ]++;
}

void work( )
{
    int i, j;
    for ( i = 0; i < n; i++ )
    {
        if ( i == 0 )
            f[ i ] = 0;
        else
            f[ i ] = f[ i - 1 ];
        for ( j = 0; j <= i; j++ )
        {
            if ( j )
            {
                if ( f[ i ] < f[ j - 1 ] + min( v[ j ][ i ], i - j + 1 ) )
                    f[ i ] = f[ j - 1 ] + min( v[ j ][ i ], i - j + 1 );
            }
            else
                if ( f[ i ] < min( v[ j ][ i ], i - j + 1 ) )
                    f[ i ] = f[ j - 1 ] + min( v[ j ][ i ], i - j + 1 );
        }
    }
    printf("%d\n", f[ n - 1 ]);
}

int main( )
{
    int t;
    scanf("%d", &t);
    while ( t-- )
    {
        init( );
        work( );
    }
    return 0;
}
